package com.eyas.business.service.rechargeinfo;

import com.eyas.business.config.webmvc.exception.custom.uncheck.EyasBusinessException;
import com.eyas.business.config.webmvc.interceptor.LoginInterceptor;
import com.eyas.business.model.jdbcEntity.table.RechargeInfo;
import com.eyas.business.model.jdbcEntity.table.UserInfo;
import com.eyas.business.model.jpa.AdminInfo;
import com.eyas.business.model.pojo.rechargeinfo.QueryParamDTO;
import com.eyas.business.model.pojo.rechargeinfo.QueryResultDTO;
import com.eyas.core.model.page.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import static com.eyas.core.jdbc.utils.CommonJdbcUtils.commonJdbcUtils;

import javax.servlet.http.HttpSession;
import java.util.Calendar;
import java.util.Date;

/**
 * @Auther: 王龙龙
 * @Date: 2019/3/16 11:16
 * @Description:
 */
@Service
@Transactional
public class RechargeInfoServiceImpl implements RechargeInfoService {
    @Override
    public void saveRechageInfo(RechargeInfo rechargeInfo, HttpSession session) {
        AdminInfo adminInfo = (AdminInfo)session.getAttribute(LoginInterceptor.LONGIN_SESSION_NAME);
        rechargeInfo.setAddid(adminInfo.getAdminnid());
        rechargeInfo.setAddtime(new Date());
        commonJdbcUtils.insert(rechargeInfo);
        UserInfo userInfo = commonJdbcUtils.queryObject("select * from userinfo where userid=?",UserInfo.class,rechargeInfo.getUserid());
        if(userInfo==null)
            throw new EyasBusinessException("会员信息获取不到，请检查");
        if(rechargeInfo.getRechargetype().equals("2")) {//充值体验课程
            userInfo.setTiyannums(userInfo.getTiyannums()==null?(0+rechargeInfo.getNums()):(userInfo.getTiyannums()+rechargeInfo.getNums()));
            userInfo.setTiyansums(userInfo.getTiyansums()==null?(0+rechargeInfo.getNums()):(userInfo.getTiyansums()+rechargeInfo.getNums()));
            userInfo.setTotleamount(userInfo.getTotleamount()==null?(0+rechargeInfo.getAmount()):(userInfo.getTotleamount()+rechargeInfo.getAmount()));
            userInfo.setRechargenum(userInfo.getRechargenum()==null?(0+1):(userInfo.getRechargenum()+1));
        }else {//充值正常课程
            userInfo.setRestclass(userInfo.getRestclass()==null?(0+rechargeInfo.getNums()):(userInfo.getRestclass()+rechargeInfo.getNums()));
            userInfo.setTotleclass(userInfo.getTotleclass()==null?(0+rechargeInfo.getNums()):(userInfo.getTotleclass()+rechargeInfo.getNums()));
            userInfo.setTotleamount(userInfo.getTotleamount()==null?(0+rechargeInfo.getAmount()):(userInfo.getTotleamount()+rechargeInfo.getAmount()));
            userInfo.setRechargenum(userInfo.getRechargenum()==null?(0+1):(userInfo.getRechargenum()+1));
        }

        //根据充值月数，修改会员的有效截止期
        /*有效期计算规则：
        * 充值操作时的系统时间:X
        * 充值月数:Y
        * 会员有效截止期:Z=X.month+Y
        * */
        if(rechargeInfo.getValidmonths()!=null&&rechargeInfo.getValidmonths().intValue()>0) {
            Date now = new Date();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(now);
            calendar.add(Calendar.MONTH,rechargeInfo.getValidmonths());
            userInfo.setDeadline(calendar.getTime());
        }
        commonJdbcUtils.update(userInfo);
    }

    @Override
    public PageResult<QueryResultDTO> queryRechargeInfo(QueryParamDTO paramDTO, int page, int limit) {
        StringBuffer sb = new StringBuffer("");
        sb.append(" select a.rechargeid,a.userid,a.nums,a.amount,a.addid,a.addtime,a.comments,b.username as adminname,a.validmonths, ")
                .append(" case when a.rechargetype='1' then '正常课时' else '体验课时' end as rechargetype ")
                .append(" from rechargeinfo a,admininfo b ")
                .append(" where a.userid=? and a.addid=b.adminnid ")
                .append(" order by addtime desc,rechargeid desc ");
        return commonJdbcUtils.queryForPage(sb.toString(),QueryResultDTO.class,page,limit,paramDTO.getUserid());
    }
}
